package com.ks.utils.webservice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thanone.czrk.service.ws.CzrkWebService;
import com.thanone.netmanager.service.ws.OhwgWebService;

/**
 * @author pks
 * @version 2018年11月27日
 * 
 * 	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-frontend-jaxws</artifactId>
		<version>2.7.6</version>
	</dependency>
	<dependency>
		<groupId>org.apache.cxf</groupId>
		<artifactId>cxf-rt-transports-http</artifactId>
		<version>2.7.6</version>
	</dependency>
 * 
 */
public class SUtilWs {
	
	private static Logger logger = LoggerFactory.getLogger(SUtilWs.class);
	
	public static String invoke(String wsdl_url,String method,String user_name,String pass_word,String... params){
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

        Client client = dcf.createClient(wsdl_url);
        // 需要密码的情况需要加上用户名和密码
        if (StringUtils.isNotBlank(user_name) && StringUtils.isNotBlank(pass_word)) {
        	client.getOutInterceptors().add(new ClientLoginInterceptor(user_name, pass_word));
        } 
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke(method,params);
            return String.valueOf(objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return null;
	}
	
	public static void main(String[] args) throws Exception {
//		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
//		fac.setServiceClass(OhwgWebService.class);
//		fac.setAddress("http://192.168.1.158:8080/netmanager/ohwg/User?wsdl");
//		final OhwgWebService cs = (OhwgWebService) fac.create();
//		System.err.println(System.currentTimeMillis());
//		List<Future<String>> futures = new ArrayList<Future<String>>();
//		for (int i = 0; i < 300; i++) {
//			Callable<String> task = new Callable<String>() {
//				@Override
//				public String call() throws Exception {
//					String s = cs.check("123", "12312", "4124");
//					System.out.println(s);
//					return s;
//				}
//			};
//			futures.add(executor.submit(task));
//		}
//		for (Future<String> future : futures) {
//			while (true) {
//                if (future.isDone() && !future.isCancelled()) {
//                    break;
//                } else {
//                    Thread.sleep(1000);
//                }
//            }
//		}
//		
//		System.err.println(System.currentTimeMillis());
		
//		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
//		fac.setServiceClass(CzrkWebService.class);
//		fac.setAddress("http://192.168.1.158:28080/czrk/Query?wsdl");
//		final CzrkWebService cs = (CzrkWebService) fac.create();
//		String result = cs.czrk("330304199003288716", "3123", "123","330324198211124530","杨展望");
//		System.out.println(result);
		
//		JaxWsProxyFactoryBean fac = new JaxWsProxyFactoryBean();
//		fac.setServiceClass(OhwgWebService.class);
//		fac.setAddress("http://192.168.1.158:8080/netmanager/ohwg/User?wsdl");
//		final OhwgWebService cs = (OhwgWebService) fac.create();
//		String s = cs.getToken("ohedu_AC", "oheduac2yun!");
//		System.out.println(s);
		
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://192.168.1.158:8080/netmanager/ohwg/User?wsdl");
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("getToken","ohedu_AC", "oheduac2yun!");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
	}
}

